import { native, sys,screen,Size,macro,view, ResolutionPolicy } from "cc"
import { DEBUG } from "cc/env";

export default class GameUtils{
    /**
     * 打开一个网址
     * @param url 
     */
    static openURL(url:string){
        console.log("openURL > "+url)
        if(sys.openURL){
            sys.openURL(url)
        }else{
            window.open(url)
        }
    }
    /**
     * 将字节转byte,kb,mb,GB
     * @param byte 
     * @returns 
     */
    public static convertByte(byte: number):string{
        let size = "";
        if (byte < 0.1 * 1024) {
          // 小于0.1KB，则转化成B
          size = `${byte.toFixed(2)}B`;
        } else if (byte < 0.1 * 1024 * 1024) {
          // 小于0.1MB，则转化成KB
          size = `${(byte / 1024).toFixed(2)}KB`;
        } else if (byte < 0.1 * 1024 * 1024 * 1024) {
          // 小于0.1GB，则转化成MB
        //   size = `${(byte / (1024 * 1024)).toFixed(2)}MB`;
        size = `${(byte / 1024).toFixed(2)}MB`;
        } else {
          // 其他转化成GB
        //   size = `${(byte / (1024 * 1024 * 1024)).toFixed(2)}GB`;
          size = `${(byte / 1024).toFixed(2)}GB`;
        }
      
        const sizeStr = `${size}`; // 转成字符串
        const index = sizeStr.indexOf("."); // 获取小数点处的索引
        const dou = sizeStr.substring(index + 1, 2); // 获取小数点后两位的值
        // eslint-disable-next-line eqeqeq
        if (dou == "00") {
          // 判断后两位是否为00，如果是则删除00
          return sizeStr.substring(0, index) + sizeStr.substring(index + 3, 2);
        }
        return size;
    }
     /**
     * 金额数字转换,千分号+2个小数
     */
     public static getAmountToString(num: number, fixed: number = 2): string {
        let numToFixed = Number(num).toFixed(2).split('.');
        let sprs = Number(numToFixed[0]).toLocaleString();
        let str = sprs + '.' + numToFixed[1];
        return str;
    }
    /**
     * 字符串是否为空
     * @param str 字符串
     * @param type 去空格标识
     * @returns
     */
    public static isEmpty(str: string): boolean {
        if (!str || typeof str !== 'string' || str.length < 1) {
            return true;
        }
        return this.trim(str).length < 1;
    }
    /**
     * 去掉字符串中空格
     * @param str 字符串
     * @param type 去空格标识
     * @returns
     */
    public static trim(str: string, type: number = 0): string {
        if (!str || typeof str !== 'string' || str.length < 1) {
            return '';
        }

        switch (type) {
            case 0: return str.replace(/(^\s*)|(\s*$)/g, "");
            case 1: return str.replace(/(^\s*)/g, "");;
            case 2: return str.replace(/(\s*$)/g, "");;
        }
        return str;
    }
    /**
     * 获取bundle 资源路径
     * @param bundleName 
     * @returns 
     */
    public static getBundlePath(bundleName:string):string{
      if(DEBUG)return bundleName
      if(sys.isNative){
        return `${native.fileUtils.getWritablePath()}bundles/${bundleName}`;
      }
      return `${localStorage.getItem("ResURL")}/${GameUtils.getPlatform()}/${bundleName}`
    }
    /**
     * 获取平台字符串
     * @returns 
     */
    public static getPlatform():string{
      if(sys.isNative){
        if(!sys.isBrowser){
          return "h5"
        }
        switch(sys.platform){
          case sys.Platform.ANDROID:
            return "android"
          break
          case sys.Platform.IOS:
            return "ios"
          break
          case sys.Platform.OHOS://操作系统 - 鸿蒙
            return "ohos"
          break
        }
      }
      return "web"
    }

}